<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_Zend
 * @subpackage Validate
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * DataAdapter for Unique validator that implements Doctrine.
 *
 * @package SmartL_Zend
 * @subpackage Validate
 */
class SmartL_Zend_Validate_Unique_DataAdapter_Doctrine {
	/**
	 * @var string
	 */
	private $_recordName = null;
	/**
	 * @var string
	 */
	private $_columnName = null;
	
	/**
	 * In config under index
	 * 'recordName' is name of record representing searched table.
	 * 'columnName' is name of column which should be compared with given value.
	 *
	 * @param array $config
	 */
	public function __construct(array $config = array()) {
		if (! isset ( $config ['recordName'] )) {
			throw new Exception ( );
		}
		if (! isset ( $config ['columnName'] )) {
			throw new Exception ( );
		}
		
		$this->_recordName = $config ['recordName'];
		$this->_columnName = $config ['columnName'];
	}
	
	/**
	 * Interface method implementation.
	 *
	 * @param string $value
	 * @return boolean
	 */
	public function isUnique($value) {
		$query = new Doctrine_Query ( );
		$result = $query->from ( $this->_recordName . ' t' )->select ( "t.{$this->_columnName}" )->where ( "t.{$this->_columnName} = ?", $value )->execute ();
		
		return (count ( $result ) == 0);
	}
}